<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    下面这个UI,如何点击每一列的时候alert其index
    <ul id="test">
        <li>这是第一条</li>
        <li>这是第二条</li>
        <li>这是第三条</li>
    </ul>
</body>
<script>
    // 方法一：
    var lis = document.getElementById('test').getElementsByTagName('li');
    for (var i = 0; i < 3; i++) {
        lis[i].index = i;
        lis[i].onclick = function () {
            alert(this.index);
        }

        //方法二：
        var lis = document.getElementById('test').getElementsByTagName('li');
        for (var i = 0; i < 3; i++) {
            lis[i].index = i;
            lis[i].onclick = (function (a) {
                return function () {
                    alert(a);
                }
            })(i);
        }
    }
</script>

</html>